/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","sysmanMain", "bizProto","btnServ","flowServ"],
		function($,datagridServ, sysmanMain, bizProto,btnServ,flowServ) {

			'use strict';

			var _bizProto,_moduleConfig,_buttonConfig,
				//锚记datagrid目标元素
				$dg,
				//初始化后的datagrid dom对象
				$grid,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,$edit_window_height=700,$edit_window_width=700,window_btn,
				
				//流程执行信息grid
				_historicGrid,
				//业务表单对象
				$form
				
				
			var public_method = {				
				
				init:function(doc){
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					private_method.init(doc);					
				}
				
			}

			var private_method = {
				init : function(doc) {
					if (window.console) {
					console.info('bugEdit modules gets ready and been loaded.');
					}
					
					_bizProto = new bizProto({doc:doc,rootId:'panel-bug',panelSelector:'div.well',subSystemCode:'sysman',moduleCode:'bug'})
					_buttonConfig={										    
						 "保存":private_method.submit,
						 "取消":private_method.cancel,
						 "发送":private_method.flowSend,
						 "签收":private_method.flowSign						 
					}
					
					private_method.initPath();
					private_method.initElement(_bizProto.pageDoc)
					
				},
				
				//初始化路径
				initPath:function(){
					
				},				
				
				//初始化元素
				initElement:function(doc){					
					
					$grid = $(doc).find("#bug-dg");					

					// 编辑窗口初始化
					$edit_window = $('#bug-win')					
					$form = $($edit_window).find('form')
					
					// 编辑窗口初始化					
					var editTd = $($edit_window).find('#tb-bugEdit div')
					
					var _param = {
						bizcode:_bizProto.bizCode,
						rootPath:_bizProto.rootPath,
						container:editTd,
						callback:private_method.bindBtnEvent}
					btnServ.generateEditBtn(_param);
					
					
					//加载流程执行信息
					var selectedRecord=$grid.datagrid('getSelected');
					if(selectedRecord){
						$form.form('load',selectedRecord)
						_historicGrid= $($edit_window).find('table.historics')
						_historicGrid.datagrid({						
							url : _bizProto.flowHistoricUrl,
							queryParams:{flowInstId:selectedRecord.flowInstId}								
						});
					}
				},
				
				//绑定按钮事件
				bindBtnEvent:function(){
					
					window_btn = $($edit_window).find('a.function-btn')
					$(window_btn).unbind('click')
					$(window_btn).bind('click',	private_method.function_btn_listenner)
					
				},				
				
				// 功能按钮监听函数
				function_btn_listenner : function(e) {

					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

				},
				
				// 提交form
				submit : function() {
					if (window.console) {
						console.info('bugEdit submit on air ...');
					}
					var _param = {form:$form,grid:$grid,url:_bizProto.flowSaveUrl}
					if(_moduleConfig.flowid){
						_param.flowid = _moduleConfig.flowid						
					}
					if(_moduleConfig.fullBizcode){						
						_param.fullBizcode = _moduleConfig.fullBizcode
					}
					datagridServ.save(_param)					
				},
				//
				cancel : function() {
					$($edit_window).window('close');
				},
				//签收
				flowSign:function(){
					var _data = private_method.getFlowMsg();
					$.extend(_data,{refreshGrid:_historicGrid,flowSignUrl:_bizProto.flowSignUrl})
					flowServ.sign(_data);
				},
				//发送
				flowSend:function(){
					var _data = private_method.getFlowMsg();
					$.extend(_data,{refreshGrid:$grid,flowTaskCompleteUrl:_bizProto.flowTaskCompleteUrl,closeWindow:$edit_window})
					flowServ.taskComplete(_data);
				},
				getFlowMsg:function(){
					var instElement =$($edit_window).find('form #flowInstId')
					var taskElement =$($edit_window).find('form #flowTaskId')
					var signElement =$($edit_window).find('fieldset.form-operator input.signInfo')
					var _data = {flowInstId:$(instElement).val(),flowTaskId:$(taskElement).val(),signInfo:$(signElement).val()}
					return _data;
				}
				

			};
			
			return public_method;

		});